package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequiredPermission(name = "角色页面",expremission = "role:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){//因为要看到他的数据,所以要放在作用域里面,所以得用到它的addattribute
        model.addAttribute("pageInfo",roleService.query(qo));      //因为要用到分页所以这里查询全部的就不要了
        /*List<Role> roles = roleService.listall();
            model.addAttribute("depatrments",roles);*/
            return "role/list";//因为已经配置了视图解析器,所以省略了前面的WEB-INF/views/ 和后面的.jsp
    }

    @RequiredPermission(name = "角色删除",expremission = "role:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id !=null){
            roleService.delete(id);
        }
        return "redirect:/role/list";//就像以前一样,删除后要除定向会list查找全部的页面
    }


    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Permission> listall = permissionService.listall();
        model.addAttribute("permission",listall);

        if (id !=null){
            model.addAttribute("role",roleService.get(id));

        }
        return "role/input";
    }

    @RequiredPermission(name = "角色新增/编辑",expremission = "role:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long []ids){
            if(role.getId()!=null){//判断你进来的是否带有id,如果带有id的就肯定是修改的了
                roleService.update(role,ids);
            }
            else {
                roleService.save(role,ids);
            }
        return "redirect:/role/list";
    }
}
